package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.common.result.Result;
import com.system.runtai.entity.*;
import com.system.runtai.service.*;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.asn1.cms.PasswordRecipientInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 需求专区动态 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "需求专区动态")
@RestController
@RequestMapping("/runtai/need")
public class NeedController {

    @Autowired
    private NeedService needService;

    @Autowired
    private IpaEnterService ipaEnterService;

    @Autowired
    private NeedCollectService needCollectService;

    @Autowired
    private UpvoteService upvoteService;

    @Autowired
    private AppletUserService appletUserService;

    @Autowired
    private NeedDiscussService needDiscussService;

    @Autowired
    private DemandZoneService demandZoneService;

    @Autowired
    private HomePageService homePageService;


    @ApiOperation(value = "后台：需求专区动态")
    @GetMapping("needht")
    public Result<IPage<Need>> needht(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){
        Page<Need> recommendPage = new Page<>(page,limit);
        IPage<Need> iPage = needService.page(recommendPage);
        return Result.ok(iPage);
    }

    @ApiOperation(value = "程序：需求专区动态轮播图")
    @GetMapping("needPictore")
    public Result<HomePage> needPictore(){
        LambdaQueryWrapper<HomePage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HomePage::getStart,3);
        HomePage list = homePageService.getOne(wrapper);
        return Result.ok(list);
    }


    @ApiOperation(value = "所有需求专区动态(最新推荐)")
    @GetMapping("needList")
    public Result<IPage<Need>> needList(@RequestParam(required = false) String name,@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){
        List<Need> list111 = null;
        String s = AuthContextUtil.get();

//        if(type==1){
            if (StringUtils.isEmpty(name)) {
                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
                wrapper.orderByDesc(Need::getCreateTime);
                List<Need> list = needService.list(wrapper);

                Page<Need> recommendPage = new Page<>(page,limit);
                IPage<Need> iPage = needService.page(recommendPage,wrapper);
                for (Need record : iPage.getRecords()) {
                    LambdaQueryWrapper<Upvote> wrapper3 = new LambdaQueryWrapper<>();
                    wrapper3.eq(Upvote::getAppletUserId,Integer.valueOf(s)).eq(Upvote::getCommentid,2).eq(Upvote::getDynamicid,record.getId());
                    Upvote one = upvoteService.getOne(wrapper3);

                    if (one!=null){
                        record.setLike(1);
                    }else {
                        record.setLike(0);
                    }

                    LambdaQueryWrapper<NeedCollect> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(NeedCollect::getAppletUserId,Integer.valueOf(s)).eq(NeedCollect::getNeedId,record.getId());
                    NeedCollect one1 = needCollectService.getOne(wrapper2);

                    if (one1!=null){
                        record.setCollect(1);
                    }else {
                        record.setCollect(0);
                    }
                }

                return Result.ok(iPage);

            }else {
                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
                wrapper.orderByDesc(Need::getCreateTime).like(Need::getNickName,name);
                List<Need> list = needService.list(wrapper);
                Page<Need> recommendPage = new Page<>(page,limit);
                IPage<Need> iPage = needService.page(recommendPage,wrapper);
                for (Need record : iPage.getRecords()) {
                    LambdaQueryWrapper<Upvote> wrapper3 = new LambdaQueryWrapper<>();
                    wrapper3.eq(Upvote::getAppletUserId,Integer.valueOf(s)).eq(Upvote::getCommentid,2).eq(Upvote::getDynamicid,record.getId());
                    Upvote one = upvoteService.getOne(wrapper3);

                    if (one!=null){
                        record.setLike(1);
                    }else {
                        record.setLike(0);
                    }

                    LambdaQueryWrapper<NeedCollect> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(NeedCollect::getAppletUserId,Integer.valueOf(s)).eq(NeedCollect::getNeedId,record.getId());
                    NeedCollect one1 = needCollectService.getOne(wrapper2);

                    if (one1!=null){
                        record.setCollect(1);
                    }else {
                        record.setCollect(0);
                    }
                }

                return Result.ok(iPage);
            }
//        }else if(type==2){
//            if (StringUtils.isEmpty(name)) {
//
//                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//                wrapper.orderByDesc(Need::getCreateTime);
//                Page<Need> recommendPage = new Page<>(page,limit);
//                IPage<Need> iPage = needService.page(recommendPage,wrapper);
//                return Result.ok(iPage);
//            }else {
//                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//                wrapper.orderByDesc(Need::getCreateTime).like(Need::getNickName,name);
//                Page<Need> recommendPage = new Page<>(page,limit);
//                IPage<Need> iPage = needService.page(recommendPage,wrapper);
//                return Result.ok(iPage);
//            }
//        }else if (type==3){
//            if (StringUtils.isEmpty(name)) {
//                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//                wrapper.orderByDesc(Need::getCreateTime);
//                List<Need> list = needService.list(wrapper);
//                Page<Need> recommendPage = new Page<>(page,limit);
//                IPage<Need> iPage = needService.page(recommendPage,wrapper);
//                return Result.ok(iPage);
//            }else {
//                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//                wrapper.orderByDesc(Need::getCreateTime).like(Need::getNickName,name);
//                List<Need> list = needService.list(wrapper);
//                Page<Need> recommendPage = new Page<>(page,limit);
//                IPage<Need> iPage = needService.page(recommendPage,wrapper);
//                return Result.ok(iPage);
//            }
//        }
//        PageInfo<Need> pageInfo = null;
//        return Result.fail(pageInfo).message("查询失败！数据为空！");
//        Page<Need> recommendPage = new Page<>(page,limit);
//        IPage<Need> iPage = null;
//        return Result.fail(iPage);
    }


    @ApiOperation(value = "所有需求专区动态(热门排行)")
    @GetMapping("needListHot")
    public Result<IPage<Need>> needListHot(@RequestParam(required = false) String name,@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){
        String s = AuthContextUtil.get();
//        if(type==1){
            if (StringUtils.isEmpty(name)) {
                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
                wrapper.orderByDesc(Need::getNumber);
                List<Need> list = needService.list(wrapper);
                Page<Need> recommendPage = new Page<>(page,limit);
                IPage<Need> iPage = needService.page(recommendPage,wrapper);
                for (Need record : iPage.getRecords()) {
                    LambdaQueryWrapper<Upvote> wrapper3 = new LambdaQueryWrapper<>();
                    wrapper3.eq(Upvote::getAppletUserId,Integer.valueOf(s)).eq(Upvote::getCommentid,2).eq(Upvote::getDynamicid,record.getId());
                    Upvote one = upvoteService.getOne(wrapper3);

                    if (one!=null){
                        record.setLike(1);
                    }else {
                        record.setLike(0);
                    }

                    LambdaQueryWrapper<NeedCollect> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(NeedCollect::getAppletUserId,Integer.valueOf(s)).eq(NeedCollect::getNeedId,record.getId());
                    NeedCollect one1 = needCollectService.getOne(wrapper2);

                    if (one1!=null){
                        record.setCollect(1);
                    }else {
                        record.setCollect(0);
                    }
                }

                return Result.ok(iPage);
            }else {
                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
                wrapper.orderByDesc(Need::getNumber).like(Need::getNickName,name);
                List<Need> list = needService.list(wrapper);
                Page<Need> recommendPage = new Page<>(page,limit);
                IPage<Need> iPage = needService.page(recommendPage,wrapper);

                for (Need record : iPage.getRecords()) {
                    LambdaQueryWrapper<Upvote> wrapper3 = new LambdaQueryWrapper<>();
                    wrapper3.eq(Upvote::getAppletUserId,Integer.valueOf(s)).eq(Upvote::getCommentid,2).eq(Upvote::getDynamicid,record.getId());
                    Upvote one = upvoteService.getOne(wrapper3);

                    if (one!=null){
                        record.setLike(1);
                    }else {
                        record.setLike(0);
                    }

                    LambdaQueryWrapper<NeedCollect> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(NeedCollect::getAppletUserId,Integer.valueOf(s)).eq(NeedCollect::getNeedId,record.getId());
                    NeedCollect one1 = needCollectService.getOne(wrapper2);

                    if (one1!=null){
                        record.setCollect(1);
                    }else {
                        record.setCollect(0);
                    }
                }

                return Result.ok(iPage);
            }
//        }else if(type==2){
//            if (StringUtils.isEmpty(name)) {
//
//                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//                wrapper.orderByDesc(Need::getCollect);
//                List<Need> list = needService.list(wrapper);
//                Page<Need> recommendPage = new Page<>(page,limit);
//                IPage<Need> iPage = needService.page(recommendPage,wrapper);
//                return Result.ok(iPage);
//            }else {
//                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//                wrapper.orderByDesc(Need::getCollect).like(Need::getNickName,name);
//                List<Need> list = needService.list(wrapper);
//                Page<Need> recommendPage = new Page<>(page,limit);
//                IPage<Need> iPage = needService.page(recommendPage,wrapper);
//                return Result.ok(iPage);
//            }
//        }else if (type==3){
//            if (StringUtils.isEmpty(name)) {
//                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//                wrapper.orderByDesc(Need::getCollect);
//                List<Need> list = needService.list(wrapper);
//                Page<Need> recommendPage = new Page<>(page,limit);
//                IPage<Need> iPage = needService.page(recommendPage,wrapper);
//                return Result.ok(iPage);
//            }else {
//                LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//                wrapper.orderByDesc(Need::getCollect).like(Need::getNickName,name);
//                List<Need> list = needService.list(wrapper);
//                Page<Need> recommendPage = new Page<>(page,limit);
//                IPage<Need> iPage = needService.page(recommendPage,wrapper);
//                return Result.ok(iPage);
//            }
//        }

//        Page<Need> recommendPage = new Page<>(page,limit);
//        IPage<Need> iPage = null;
//        return Result.fail(iPage).message("查询失败！数据为空！");
    }


    @ApiOperation(value = "自己发布的需求专区动态")
    @GetMapping("miList")
    public Result<IPage<Need>> miList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){
        String s = AuthContextUtil.get();
        LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Need::getAppletUserId,Integer.valueOf(s));
        Page<Need> recommendPage = new Page<>(page,limit);
        IPage<Need> iPage = needService.page(recommendPage,wrapper);
        return Result.ok(iPage);
    }


    @ApiOperation(value = "添加需求专区动态")
    @PostMapping("needById")
    public Result<Need> needById(@RequestBody Need need){
        String s = AuthContextUtil.get();
//        LambdaQueryWrapper<Need> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Need::getAppletUserId, Integer.valueOf(s));
//        Need one = needService.getOne(wrapper);

        LambdaQueryWrapper<IpaEnter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IpaEnter::getAppletUserId, Integer.valueOf(s));
        IpaEnter one = ipaEnterService.getOne(wrapper);
//        if (one.getStart()==1){
            AppletUser byId = appletUserService.getById(Integer.valueOf(s));
            need.setAppletUserId(byId.getId());
            need.setAvatar(byId.getAvatar());
            need.setNickName(one.getName());
            need.setCompany(one.getCompany());
            need.setCreateTime(LocalDateTime.now());
            need.setLike(0);
            need.setCollect(0);
            boolean save = needService.save(need);
            if (save){
                return Result.ok(need).message("添加需求专区成功");
            }
            return Result.fail(need).message("添加需求专区失败");
//        }
//        return Result.fail(need).message("入驻未通过审核，暂不能发布动态");
    }

    @ApiOperation(value = "修改已完成状态")
    @PostMapping("/updateNeed")
    public Result<Need> updateNeed(@RequestBody Need need){

        need.setStart(1);
        boolean b = needService.updateById(need);

        if (b){
            return Result.ok(need).message("已完成修改");
        }
        return Result.fail(need).message("修改失败");
    }

    @ApiOperation(value = "删除需求专区动态")
    @PostMapping("/delectNeed")
    public Result delectNeed(@RequestBody Need need){

        String s = AuthContextUtil.get();
        boolean b = needService.removeById(need.getId());
        LambdaQueryWrapper<NeedDiscuss> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NeedDiscuss::getAppletUserId,Integer.valueOf(s)).eq(NeedDiscuss::getNeedid,need.getId());
        boolean remove = needDiscussService.remove(wrapper);

        LambdaQueryWrapper<Upvote> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Upvote::getDynamicid,need.getId()).eq(Upvote::getCommentid,1).eq(Upvote::getAppletUserId,Integer.valueOf(s));
        boolean remove1 = upvoteService.remove(queryWrapper);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "需求专区动态详情，评论、点赞")
    @GetMapping("/need/{id}")
    public Result<Need> needList(@PathVariable Integer id){

        String s = AuthContextUtil.get();
        Need byId = needService.getById(id);
        LambdaQueryWrapper<NeedDiscuss> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NeedDiscuss::getNeedid,byId.getId());
        List<NeedDiscuss> list = needDiscussService.list(wrapper);

        byId.setNumber(byId.getNumber()+1);
        needService.updateById(byId);

        LambdaQueryWrapper<Upvote> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Upvote::getDynamicid,byId.getId()).eq(Upvote::getCommentid,2);
        List<Upvote> list1 = upvoteService.list(wrapper1);


        LambdaQueryWrapper<Upvote> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(Upvote::getDynamicid,byId.getId()).eq(Upvote::getCommentid,2).eq(Upvote::getAppletUserId,Integer.valueOf(s));
        Upvote upvote = upvoteService.getOne(wrapper3);
        if (upvote!=null){
            byId.setLike(1);
        }else {
            byId.setLike(0);
        }

        byId.setDiscussList(list);
        byId.setUpvoteList(list1);

        LambdaQueryWrapper<NeedCollect> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(NeedCollect::getAppletUserId,Integer.valueOf(s)).eq(NeedCollect::getNeedId,id);
        NeedCollect one = needCollectService.getOne(wrapper2);
        if (one!=null){
            byId.setCollect(1);
        }else {
            byId.setCollect(0);
        }

        return Result.ok(byId);
    }

    @ApiOperation(value = "添加需求专区动态收藏")
    @PostMapping("/saveCollect")
    public Result<NeedCollect> saveCollect(@RequestBody Need need){

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<NeedCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NeedCollect::getNeedId,need.getId()).eq(NeedCollect::getAppletUserId,Integer.valueOf(s));
        NeedCollect one = needCollectService.getOne(wrapper);

        if (one!=null){
            return Result.ok(one);
        }else {
            NeedCollect needCollect = new NeedCollect();
            needCollect.setCreateTime(LocalDateTime.now());
            needCollect.setNeedId(need.getId());
            needCollect.setAppletUserId(Integer.valueOf(s));
            boolean save = needCollectService.save(needCollect);

            if (save) {

                need.setCollectNum(need.getCollectNum()+1);
                needService.updateById(need);
                return Result.ok(needCollect).message("添加行业收藏成功");
            }
            return Result.fail(needCollect).message("添加行业收藏失败");
        }
    }

    @ApiOperation(value = "取消需求专区动态收藏")
    @PostMapping("/delectNeedCollect")
    public Result delectNeedCollect(@RequestBody Need need){

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<NeedCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NeedCollect::getNeedId,need.getId()).eq(NeedCollect::getAppletUserId,Integer.valueOf(s));
        boolean b = needCollectService.remove(wrapper);

        if (b){
            need.setCollectNum(need.getCollectNum()-1);
            needService.updateById(need);
            return Result.ok().message("取消需求专区动态收藏成功");
        }
        return Result.fail().message("取消需求专区动态收藏失败");
    }


    @ApiOperation(value = "需求专区动态添加评论")
    @PostMapping("/saveComment")
    public Result saveComment(@RequestBody NeedDiscuss needDiscuss){

        String s = AuthContextUtil.get();
        NeedDiscuss needDiscuss1 = new NeedDiscuss();
        needDiscuss1.setNeedid(needDiscuss.getFId());
        LambdaQueryWrapper<IpaEnter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IpaEnter::getAppletUserId,Integer.valueOf(s));
        IpaEnter one = ipaEnterService.getOne(wrapper);
        needDiscuss1.setAppletUserId(Integer.valueOf(s));
        needDiscuss1.setNickName(one.getName());
        needDiscuss1.setCompany(one.getCompany());
        needDiscuss1.setIntro(needDiscuss.getIntro());
        AppletUser byId = appletUserService.getById(Integer.valueOf(s));
        needDiscuss1.setAvatar(byId.getAvatar());
        boolean save = needDiscussService.save(needDiscuss1);
        if (save){
            Need byId1 = needService.getById(needDiscuss.getFId());
            byId1.setCommentNum(byId1.getCommentNum()+1);
            needService.updateById(byId1);
            return Result.ok(needDiscuss1).message("需求专区动态添加评论成功");
        }
        return Result.fail().message("需求专区动态添加评论失败");
    }


    @ApiOperation(value = "需求专区动态删除评论")
    @PostMapping("/delectCommentPinglun")
    public Result delectComment(@RequestBody NeedDiscuss needDiscuss){
        String s = AuthContextUtil.get();
        LambdaQueryWrapper<NeedDiscuss> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NeedDiscuss::getAppletUserId,Integer.valueOf(s)).eq(NeedDiscuss::getNeedid,needDiscuss.getNeedid());
        boolean b = needDiscussService.remove(wrapper);

        if (b){

            Need byId = needService.getById(needDiscuss.getNeedid());
            byId.setCommentNum(byId.getCommentNum()-1);
            return Result.ok().message("删除需求专区动态的评论成功");
        }
        return Result.fail().message("删除需求专区动态的评论失败");
    }

    @ApiOperation(value = "需求专区动态添加点赞")
    @PostMapping("/savaUpvote")
    public Result savaUpvote(@RequestBody Need posted) {

        Upvote up = new Upvote();

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<Upvote> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Upvote::getDynamicid,posted.getId()).eq(Upvote::getAppletUserId,Integer.valueOf(s));

        Upvote one = upvoteService.getOne(wrapper);
        if (one!=null){
            return Result.ok(one);
        }else {

            up.setCommentid(2);
            up.setDynamicid(posted.getId());
            AppletUser byId = appletUserService.getById(Integer.valueOf(s));
            up.setAvatar(byId.getAvatar());
            up.setAppletUserId(byId.getId());
            boolean save = upvoteService.save(up);
            if (save) {
                Need byId1 = needService.getById(posted.getId());
                byId1.setUpvoteNum(byId1.getUpvoteNum() + 1);
                boolean b = needService.updateById(byId1);

                return Result.ok().message("点赞成功");
            }
            return Result.fail().message("点赞失败");
        }
    }


    @ApiOperation(value = "需求专区动态取消点赞")
    @PostMapping("/delectUpvote")
    public Result delectUpvote(@RequestBody Need posted){
        String s = AuthContextUtil.get();
        LambdaQueryWrapper<Upvote> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Upvote::getAppletUserId,Integer.valueOf(s)).eq(Upvote::getDynamicid,posted.getId());
        boolean b = upvoteService.remove(wrapper);

        if (b){

            Need byId = needService.getById(posted.getId());
            byId.setUpvoteNum(byId.getUpvoteNum()-1);
            return Result.ok().message("取消需求专区动态的点赞成功");
        }
        return Result.fail().message("取消需求专区动态的点赞失败");
    }


    @ApiOperation(value = "需求专区动态分享数量增加")
    @PostMapping("/saveShareNum")
    public Result<Need> saveShareNum(@RequestBody Need posted){

        Need byId = needService.getById(posted.getId());
        byId.setShareNum(byId.getShareNum()+1);
        boolean b = needService.updateById(posted);
        if (b){
            return Result.ok(byId).message("增加分享数量成功");
        }

        return Result.fail(byId).message("增加分享数量失败");
    }

}

